package com.jrx.anytxn.transaction.timer.job.timer;

import com.jrx.anytxn.common.uitl.DateUtils;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuzb
 * @date 2024年03月07日 15:16
 */
@RestController
@RequestMapping(value = "/timer/job")
public class JobLauncherController {

    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @GetMapping(value = "/exec")
    public String exec(@RequestParam(value = "jobName") String jobName) throws Exception {
        JobParameters jobParameter = new JobParametersBuilder()
                .addString("name", jobName)
                .addString("str", DateUtils.formatDateTime())
                .toJobParameters();
        Job job = this.applicationContext.getBean(jobName, Job.class);
        jobLauncher.run(job, jobParameter);
        return "调用成功";
    }
}
